﻿////////////////////////////////////////////////////////////////////////
//
// Module           : DateTime.js
// Description      : Return formatted Date/Time
// Developer        : Alexander Bell, USA (www.alexanderbell.us)
// Copyright(C)     : 2001-2008 Alexander Bell
// Version          : V. 2.02
// Revision History:
// 06/01/2001 - original version
// 12/05/2008 - file restructring/modularity added
////////////////////////////////////////////////////////////////////////

// function to return date/time string 
//****************************************************************
// ShowDate :   include DATE
// ShowTime :   include TIME
// md       :   DATE/MONTH format: MM/DD (default) or DD/MM
// Clock24  :   12 or 24 HR Clock
// ShowMS   :   include milliseconds (1 digit)
// LocUTC   :   use local or UTC time
function DateAndTime(ShowDate, 
                     ShowTime, 
                     md,
                     Clock24, 
                     ShowMS,
                     LocUTC)
{
    //vars
    var yyyy;       // Year
    var mm;         // Month
    var dd;         // Day
    var h;          // Hours
    var m;          // Minutes
    var s;          // Seconds
    var ms;         // Milliseconds
    var ampm;       // AM or PM
    var strD;       // temp string for Date
    var strT;       // temp string for Time
    var ret="";     // return string

    // new DATE and TIME object
    now = new Date();
    
    // if parameter not empty, format and add date
    if (ShowDate!=""){
        // UTC DATE
        if (LocUTC=="UTC") {
	        yyyy =now.getUTCFullYear();
	        mm =now.getUTCMonth()+1;
	        dd =now.getUTCDate();
	    }
	    // local DATE(Default)
	    else {
	    	yyyy =now.getYear();
	        mm =now.getMonth()+1;
	        dd =now.getDate();
	    }
    
        // use 2 digits MM/DD format
        if (mm<10) mm="0"+mm;
        if (dd<10) dd="0"+dd;
        
        // use MMDD or DDMM (Default: MMDD)
        if (md=="ddmm") {strD=dd+'/'+mm+'/'+yyyy;}
        else {strD=mm+'/'+dd+'/'+yyyy;}
        
        //DATE STRING
        ret=strD + " ";
    }

    // if parameter not empty, format and add time
    if (ShowTime != "")
    {
        // UTC time
        if (LocUTC=="UTC") {
	        h =now.getUTCHours();
	        m =now.getUTCMinutes();
	        s =now.getUTCSeconds();
	        ms=now.getUTCMilliseconds();
	    }
	    // local time(Default)
	    else {
	        h =now.getHours();
	        m =now.getMinutes();
	        s =now.getSeconds();
	        ms=now.getMilliseconds();
	    }
        // use 2 digits for H, M and S
        if (h<10) h="0"+h;
        if (m<10) m="0"+m;
        if (s<10) s="0"+s;

        // add milliseconds
        if (ShowMS != "") { ms = "." + Math.round(ms / 100 - 0.5); } else {ms = ""; }

        // use 12/24 Clock (default 12 hr clock)
        if (Clock24=="24") {strT =h+':'+m+':'+s + ms;}
        else {
	        ampm= (h>=12) ? "PM":"AM";
	        if (h>12) 	h-=12;
	        if (h==0) 	h=12;
	        strT= h + ':' + m + ':' + s + ms + ' ' + ampm;
	     }
	    // time string
        ret += strT;
    }
    
    // optional UTC prefix
    ret=((LocUTC=="UTC")? "UTC " : "" ) + ret;

    // return string
    return(ret);
}
//////////////////////////////////////////////////////////////////